package com.shouhuyun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.shouhuyun.dao.OrderSettingDao;
import com.shouhuyun.pojo.OrderSetting;
import com.shouhuyun.service.OrderSettingService;
import com.shouhuyun.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    //  调用 dao接口
    @Autowired
    private OrderSettingDao orderSettingDao;

    //预约服务设置
    public void add(List<OrderSetting> orderSettingList) {

        if(orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                //检查此数据（日期）是否存在
                long count =
                        orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){
                    //已经存在，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据日期 范围查询预约设置数据 (查询每个月的)
    public List<Map> getOrderSettingByMonth(String date) {
//        设置月份的开始跟结束
        String dateBegin = date + "-1"; //2019-12-1
        String dateEnd = date + "-31"; //2019-12-31
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);

        /**
         * 可以用map 封装一下
         */
        List<Map> data = orderSettingDao.getOrderSettingByMonth2(map);

/*  课程里的代码

        //查找该月份所有信息
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        //创建一个空的 集合 用来接收 每天的预约信息
        List<Map> data = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Map orderSettingMap = new HashMap();
                orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期 几号
                orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
                orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数

                data.add(orderSettingMap);
            }
        }
*/

        return data;
    }

    //根据日期设置对应的预约设置数据
    public void editNumberByDate(OrderSetting orderSetting) {
//        查询日期是否存在
        long cout = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (cout > 0){
//            如果日期已经进行了预约设置  进行修改
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
//            如果日期没有进行预约设置  进行添加操作你
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public void clearOrder() {
        Date date = DateUtils.getMonth4ThisMonth(-6);
        orderSettingDao.clearOrderSetting(date);
    }
}



















